package com.xph.core.common.json;

import org.apache.log4j.Logger;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.json.JSONObject;

public class SyncJsonThread extends Thread {
	private static Logger log=Logger.getLogger(SyncJsonThread.class);
	private RestClient restclient;
	private String url;
	private boolean isGet;
	private JSONObject json;
	/**
	 * @param restclient
	 * @param url
	 */
	public SyncJsonThread(RestClient restclient, String url,boolean isGet,JSONObject json) {
		super();
		this.restclient = restclient;
		this.url = url;
		this.isGet = isGet;
		this.json = json;
	}

	@Override
    public void run() {
        try {
        	ClientResponse response = null;
        	log.info("sys order result, url:"+ url);
			Resource resource = restclient.resource(this.url);
			if(isGet){
				response = resource.contentType(RestClientUtil.DEFAUT_CONTENTTYPE).get();
			}else{
		        response = resource.contentType(RestClientUtil.DEFAUT_CONTENTTYPE)
                .accept(RestClientUtil.DEFAUT_CONTENTTYPE).post(this.json);
			}
			if(response!=null){
		        JSONObject obj = response.getEntity(JSONObject.class);
		        if(obj!=null){
		        	log.info("异步接口调用rest接口地址为"+this.url+"||异步接口调用返回值为"+obj.toString());
		        }
		        response = null;
			}else{
				if(isGet){
				    log.error("异步接口调用返回值为空:"+this.url);
				}else{
					log.error("异步接口调用返回值为空:"+this.url+"|"+this.json.toString());
				}
			}
		} catch (Exception e) {
			log.error("异步调用接口异常url:"+this.url+",json:"+json, e);
		}
        this.restclient = null;
    }

}
